

<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Building a Distribution of LLVM &#8212; LLVM 9 documentation</title>
    <link rel="stylesheet" href="_static/llvm-theme.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    <script id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
    <script src="_static/jquery.js"></script>
    <script src="_static/underscore.js"></script>
    <script src="_static/doctools.js"></script>
    <script src="_static/language_data.js"></script>
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="next" title="Remarks" href="Remarks.html" />
    <link rel="prev" title="A guide to Dockerfiles for building LLVM" href="Docker.html" />
<style type="text/css">
  table.right { float: right; margin-left: 20px; }
  table.right td { border: 1px solid #ccc; }
</style>

  </head><body>
<div class="logo">
  <a href="index.html">
    <img src="_static/logo.png"
         alt="LLVM Logo" width="250" height="88"/></a>
</div>

    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="Remarks.html" title="Remarks"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="Docker.html" title="A guide to Dockerfiles for building LLVM"
             accesskey="P">previous</a> |</li>
  <li><a href="http://llvm.org/">LLVM Home</a>&nbsp;|&nbsp;</li>
  <li><a href="index.html">Documentation</a>&raquo;</li>

        <li class="nav-item nav-item-this"><a href="">Building a Distribution of LLVM</a></li> 
      </ul>
    </div>


    <div class="document">
      <div class="documentwrapper">
          <div class="body" role="main">
            
  <div class="section" id="building-a-distribution-of-llvm">
<h1>Building a Distribution of LLVM<a class="headerlink" href="#building-a-distribution-of-llvm" title="Permalink to this headline">¶</a></h1>
<div class="contents local topic" id="contents">
<ul class="simple">
<li><p><a class="reference internal" href="#introduction" id="id1">Introduction</a></p></li>
<li><p><a class="reference internal" href="#general-distribution-guidance" id="id2">General Distribution Guidance</a></p>
<ul>
<li><p><a class="reference internal" href="#difference-between-install-and-install-distribution" id="id3">Difference between <code class="docutils literal notranslate"><span class="pre">install</span></code> and <code class="docutils literal notranslate"><span class="pre">install-distribution</span></code></a></p></li>
<li><p><a class="reference internal" href="#special-notes-for-library-only-distributions" id="id4">Special Notes for Library-only Distributions</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#options-for-optimizing-llvm" id="id5">Options for Optimizing LLVM</a></p></li>
<li><p><a class="reference internal" href="#options-for-reducing-size" id="id6">Options for Reducing Size</a></p></li>
<li><p><a class="reference internal" href="#relevant-cmake-options" id="id7">Relevant CMake Options</a></p></li>
</ul>
</div>
<div class="section" id="introduction">
<h2><a class="toc-backref" href="#id1">Introduction</a><a class="headerlink" href="#introduction" title="Permalink to this headline">¶</a></h2>
<p>This document is geared toward people who want to build and package LLVM and any
combination of LLVM sub-project tools for distribution. This document covers
useful features of the LLVM build system as well as best practices and general
information about packaging LLVM.</p>
<p>If you are new to CMake you may find the <a class="reference internal" href="CMake.html"><span class="doc">Building LLVM with CMake</span></a> or <a class="reference internal" href="CMakePrimer.html"><span class="doc">CMake Primer</span></a>
documentation useful. Some of the things covered in this document are the inner
workings of the builds described in the <a class="reference internal" href="AdvancedBuilds.html"><span class="doc">Advanced Build Configurations</span></a> document.</p>
</div>
<div class="section" id="general-distribution-guidance">
<h2><a class="toc-backref" href="#id2">General Distribution Guidance</a><a class="headerlink" href="#general-distribution-guidance" title="Permalink to this headline">¶</a></h2>
<p>When building a distribution of a compiler it is generally advised to perform a
bootstrap build of the compiler. That means building a “stage 1” compiler with
your host toolchain, then building the “stage 2” compiler using the “stage 1”
compiler. This is done so that the compiler you distribute benefits from all the
bug fixes, performance optimizations and general improvements provided by the
new compiler.</p>
<p>In deciding how to build your distribution there are a few trade-offs that you
will need to evaluate. The big two are:</p>
<ol class="arabic simple">
<li><p>Compile time of the distribution against performance of the built compiler</p></li>
<li><p>Binary size of the distribution against performance of the built compiler</p></li>
</ol>
<p>The guidance for maximizing performance of the generated compiler is to use LTO,
PGO, and statically link everything. This will result in an overall larger
distribution, and it will take longer to generate, but it provides the most
opportunity for the compiler to optimize.</p>
<p>The guidance for minimizing distribution size is to dynamically link LLVM and
Clang libraries into the tools to reduce code duplication. This will come at a
substantial performance penalty to the generated binary both because it reduces
optimization opportunity, and because dynamic linking requires resolving symbols
at process launch time, which can be very slow for C++ code.</p>
<div class="admonition warning" id="shared-libs">
<p class="admonition-title">Warning</p>
<p>One very important note: Distributions should never be built using the
<em>BUILD_SHARED_LIBS</em> CMake option. That option exists for optimizing developer
workflow only. Due to design and implementation decisions, LLVM relies on
global data which can end up being duplicated across shared libraries
resulting in bugs. As such this is not a safe way to distribute LLVM or
LLVM-based tools.</p>
</div>
<p>The simplest example of building a distribution with reasonable performance is
captured in the DistributionExample CMake cache file located at
clang/cmake/caches/DistributionExample.cmake. The following command will perform
and install the distribution build:</p>
<div class="highlight-console notranslate"><div class="highlight"><pre><span></span><span class="gp">$</span> cmake -G Ninja -C &lt;path to clang&gt;/cmake/caches/DistributionExample.cmake &lt;path to LLVM source&gt;
<span class="gp">$</span> ninja stage2-distribution
<span class="gp">$</span> ninja stage2-install-distribution
</pre></div>
</div>
<div class="section" id="difference-between-install-and-install-distribution">
<h3><a class="toc-backref" href="#id3">Difference between <code class="docutils literal notranslate"><span class="pre">install</span></code> and <code class="docutils literal notranslate"><span class="pre">install-distribution</span></code></a><a class="headerlink" href="#difference-between-install-and-install-distribution" title="Permalink to this headline">¶</a></h3>
<p>One subtle but important thing to note is the difference between the <code class="docutils literal notranslate"><span class="pre">install</span></code>
and <code class="docutils literal notranslate"><span class="pre">install-distribution</span></code> targets. The <code class="docutils literal notranslate"><span class="pre">install</span></code> target is expected to
install every part of LLVM that your build is configured to generate except the
LLVM testing tools. Alternatively the <code class="docutils literal notranslate"><span class="pre">install-distribution</span></code> target, which is
recommended for building distributions, only installs specific parts of LLVM as
specified at configuration time by <em>LLVM_DISTRIBUTION_COMPONENTS</em>.</p>
<p>Additionally by default the <code class="docutils literal notranslate"><span class="pre">install</span></code> target will install the LLVM testing
tools as the public tools. This can be changed well by setting
<em>LLVM_INSTALL_TOOLCHAIN_ONLY</em> to <code class="docutils literal notranslate"><span class="pre">On</span></code>. The LLVM tools are intended for
development and testing of LLVM, and should only be included in distributions
that support LLVM development.</p>
<p>When building with <em>LLVM_DISTRIBUTION_COMPONENTS</em> the build system also
generates a <code class="docutils literal notranslate"><span class="pre">distribution</span></code> target which builds all the components specified in
the list. This is a convenience build target to allow building just the
distributed pieces without needing to build all configured targets.</p>
</div>
<div class="section" id="special-notes-for-library-only-distributions">
<h3><a class="toc-backref" href="#id4">Special Notes for Library-only Distributions</a><a class="headerlink" href="#special-notes-for-library-only-distributions" title="Permalink to this headline">¶</a></h3>
<p>One of the most powerful features of LLVM is its library-first design mentality
and the way you can compose a wide variety of tools using different portions of
LLVM. Even in this situation using <em>BUILD_SHARED_LIBS</em> is not supported. If you
want to distribute LLVM as a shared library for use in a tool, the recommended
method is using <em>LLVM_BUILD_LLVM_DYLIB</em>, and you can use <em>LLVM_DYLIB_COMPONENTS</em>
to configure which LLVM components are part of libLLVM.</p>
</div>
</div>
<div class="section" id="options-for-optimizing-llvm">
<h2><a class="toc-backref" href="#id5">Options for Optimizing LLVM</a><a class="headerlink" href="#options-for-optimizing-llvm" title="Permalink to this headline">¶</a></h2>
<p>There are four main build optimizations that our CMake build system supports.
When performing a bootstrap build it is not beneficial to do anything other than
setting <em>CMAKE_BUILD_TYPE</em> to <code class="docutils literal notranslate"><span class="pre">Release</span></code> for the stage-1 compiler. This is
because the more intensive optimizations are expensive to perform and the
stage-1 compiler is thrown away. All of the further options described should be
set on the stage-2 compiler either using a CMake cache file, or by prefixing the
option with <em>BOOTSTRAP_</em>.</p>
<p>The first and simplest to use is the compiler optimization level by setting the
<em>CMAKE_BUILD_TYPE</em> option. The main values of interest are <code class="docutils literal notranslate"><span class="pre">Release</span></code> or
<code class="docutils literal notranslate"><span class="pre">RelWithDebInfo</span></code>. By default the <code class="docutils literal notranslate"><span class="pre">Release</span></code> option uses the <code class="docutils literal notranslate"><span class="pre">-O3</span></code>
optimization level, and <code class="docutils literal notranslate"><span class="pre">RelWithDebInfo</span></code> uses <code class="docutils literal notranslate"><span class="pre">-O2</span></code>. If you want to generate
debug information and use <code class="docutils literal notranslate"><span class="pre">-O3</span></code> you can override the
<em>CMAKE_&lt;LANG&gt;_FLAGS_RELWITHDEBINFO</em> option for C and CXX.
DistributionExample.cmake does this.</p>
<p>Another easy to use option is Link-Time-Optimization. You can set the
<em>LLVM_ENABLE_LTO</em> option on your stage-2 build to <code class="docutils literal notranslate"><span class="pre">Thin</span></code> or <code class="docutils literal notranslate"><span class="pre">Full</span></code> to enable
building LLVM with LTO. These options will significantly increase link time of
the binaries in the distribution, but it will create much faster binaries. This
option should not be used if your distribution includes static archives, as the
objects inside the archive will be LLVM bitcode, which is not portable.</p>
<p>The <a class="reference internal" href="AdvancedBuilds.html"><span class="doc">Advanced Build Configurations</span></a> documentation describes the built-in tooling for
generating LLVM profiling information to drive Profile-Guided-Optimization. The
in-tree profiling tests are very limited, and generating the profile takes a
significant amount of time, but it can result in a significant improvement in
the performance of the generated binaries.</p>
<p>In addition to PGO profiling we also have limited support in-tree for generating
linker order files. These files provide the linker with a suggested ordering for
functions in the final binary layout. This can measurably speed up clang by
physically grouping functions that are called temporally close to eachother. The
current tooling is only available on Darwin systems with <code class="docutils literal notranslate"><span class="pre">dtrace(1)</span></code>. It is
worth noting that dtrace is non-deterministic, and so the order file generation
using dtrace is also non-deterministic.</p>
</div>
<div class="section" id="options-for-reducing-size">
<h2><a class="toc-backref" href="#id6">Options for Reducing Size</a><a class="headerlink" href="#options-for-reducing-size" title="Permalink to this headline">¶</a></h2>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>Any steps taken to reduce the binary size will come at a cost of runtime
performance in the generated binaries.</p>
</div>
<p>The simplest and least significant way to reduce binary size is to set the
<em>CMAKE_BUILD_TYPE</em> variable to <code class="docutils literal notranslate"><span class="pre">MinSizeRel</span></code>, which will set the compiler
optimization level to <code class="docutils literal notranslate"><span class="pre">-Os</span></code> which optimizes for binary size. This will have
both the least benefit to size and the least impact on performance.</p>
<p>The most impactful way to reduce binary size is to dynamically link LLVM into
all the tools. This reduces code size by decreasing duplication of common code
between the LLVM-based tools. This can be done by setting the following two
CMake options to <code class="docutils literal notranslate"><span class="pre">On</span></code>: <em>LLVM_BUILD_LLVM_DYLIB</em> and <em>LLVM_LINK_LLVM_DYLIB</em>.</p>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>Distributions should never be built using the <em>BUILD_SHARED_LIBS</em> CMake
option. (<a class="reference internal" href="#shared-libs"><span class="std std-ref">See the warning above for more explanation</span></a>.).</p>
</div>
</div>
<div class="section" id="relevant-cmake-options">
<h2><a class="toc-backref" href="#id7">Relevant CMake Options</a><a class="headerlink" href="#relevant-cmake-options" title="Permalink to this headline">¶</a></h2>
<p>This section provides documentation of the CMake options that are intended to
help construct distributions. This is not an exhaustive list, and many
additional options are documented in the <a class="reference internal" href="CMake.html"><span class="doc">Building LLVM with CMake</span></a> page. Some key options
that are already documented include: <em>LLVM_TARGETS_TO_BUILD</em>,
<em>LLVM_ENABLE_PROJECTS</em>, <em>LLVM_BUILD_LLVM_DYLIB</em>, and <em>LLVM_LINK_LLVM_DYLIB</em>.</p>
<dl class="simple">
<dt><strong>LLVM_ENABLE_RUNTIMES</strong>:STRING</dt><dd><p>When building a distribution that includes LLVM runtime projects (i.e. libcxx,
compiler-rt, libcxxabi, libunwind…), it is important to build those projects
with the just-built compiler.</p>
</dd>
<dt><strong>LLVM_DISTRIBUTION_COMPONENTS</strong>:STRING</dt><dd><p>This variable can be set to a semi-colon separated list of LLVM build system
components to install. All LLVM-based tools are components, as well as most
of the libraries and runtimes. Component names match the names of the build
system targets.</p>
</dd>
<dt><strong>LLVM_RUNTIME_DISTRIBUTION_COMPONENTS</strong>:STRING</dt><dd><p>This variable can be set to a semi-colon separated list of runtime library
components. This is used in conjunction with <em>LLVM_ENABLE_RUNTIMES</em> to specify
components of runtime libraries that you want to include in your distribution.
Just like with <em>LLVM_DISTRIBUTION_COMPONENTS</em>, component names match the names
of the build system targets.</p>
</dd>
<dt><strong>LLVM_DYLIB_COMPONENTS</strong>:STRING</dt><dd><p>This variable can be set to a semi-colon separated name of LLVM library
components. LLVM library components are either library names with the LLVM
prefix removed (i.e. Support, Demangle…), LLVM target names, or special
purpose component names. The special purpose component names are:</p>
<ol class="arabic simple">
<li><p><code class="docutils literal notranslate"><span class="pre">all</span></code> - All LLVM available component libraries</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">Native</span></code> - The LLVM target for the Native system</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">AllTargetsAsmPrinters</span></code> - All the included target ASM printers libraries</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">AllTargetsAsmParsers</span></code> - All the included target ASM parsers libraries</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">AllTargetsDescs</span></code> - All the included target descriptions libraries</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">AllTargetsDisassemblers</span></code> - All the included target dissassemblers libraries</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">AllTargetsInfos</span></code> - All the included target info libraries</p></li>
</ol>
</dd>
<dt><strong>LLVM_INSTALL_TOOLCHAIN_ONLY</strong>:BOOL</dt><dd><p>This option defaults to <code class="docutils literal notranslate"><span class="pre">Off</span></code>: when set to <code class="docutils literal notranslate"><span class="pre">On</span></code> it removes many of the
LLVM development and testing tools as well as component libraries from the
default <code class="docutils literal notranslate"><span class="pre">install</span></code> target. Including the development tools is not recommended
for distributions as many of the LLVM tools are only intended for development
and testing use.</p>
</dd>
</dl>
</div>
</div>


            <div class="clearer"></div>
          </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="Remarks.html" title="Remarks"
             >next</a> |</li>
        <li class="right" >
          <a href="Docker.html" title="A guide to Dockerfiles for building LLVM"
             >previous</a> |</li>
  <li><a href="http://llvm.org/">LLVM Home</a>&nbsp;|&nbsp;</li>
  <li><a href="index.html">Documentation</a>&raquo;</li>

        <li class="nav-item nav-item-this"><a href="">Building a Distribution of LLVM</a></li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2003-2020, LLVM Project.
      Last updated on 2020-09-20.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 3.2.1.
    </div>
  </body>
</html>